//
//  MGModuleManager.m
//  Express
//
//  Created by lhongl on 2025/9/28.
//  Copyright © 2025 QM. All rights reserved.
//

#import "MGModuleManage.h"
#import "MGModuleProtocol.h"
@interface MGModuleManage ()

@property (nonatomic, strong) NSMutableArray *modules;

@end
@implementation MGModuleManage

+ (instancetype)sharedInstance {
   static MGModuleManage *_manager;
   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
       _manager = [[MGModuleManage alloc] init];
       _manager.modules = [[NSMutableArray alloc] init];
   });
   return _manager;
}

+ (void)registerModule:(Class)moduleClass {
   NSAssert1([moduleClass conformsToProtocol:@protocol(MGModuleProtocol)], @"[MG_DEBUG] moduleClass %@ not conform to protocol <DAModuleProtocol>", NSStringFromClass(moduleClass));
   NSAssert1([moduleClass respondsToSelector:@selector(sharedModule)], @"[MG_DEBUG] moduleClass %@ not responds selector sharedModule", NSStringFromClass(moduleClass));
   __block id module = nil;
   [[MGModuleManage sharedInstance].modules enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       if ([obj isKindOfClass:[moduleClass class]]) {
           module = obj;
           *stop = YES;
       }
   }];
   if (!module) {
       module = [moduleClass sharedModule];
       [[MGModuleManage sharedInstance].modules addObject:module];
   }
}

+ (NSArray *)allModules {
   return [MGModuleManage sharedInstance].modules;
}

@end
